package com.hh.base;

import com.hh.base.common.BaseConfig;
import com.hh.base.common.TableBean;
import com.hh.base.common.TableUtil;
import com.hh.base.common.Template;
import com.hh.base.generator.base.DefaultPomGenerator;
import com.hh.base.generator.base.DefaultWebXmlGenerator;
import com.hh.base.generator.database.MysqlSQLGenerator;
import com.hh.base.util.StringUtil;
import com.hh.base.util.excel.ExcelUtil;
import com.hh.base.util.excel.TableRow;
import com.hh.base.util.file.FileUtil;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Created by liuhanhui on 2017/11/1.
 */
public class Starter {
    public static void main(String[] args) throws IOException {
System.out.println( System.getProperty("os.name") );

        //创建项目文件夹
        String destDirName = BaseConfig.PATH_PROJECT + "/" + BaseConfig.ARTIFACTID;
        FileUtil.createDir(destDirName);
        FileUtil.createDir(destDirName + "/src/main/webapp/WEB-INF");
        FileUtil.createDir(destDirName + "/src/main/resources");
        FileUtil.createDir(destDirName + "/doc/sql");
        String packagePath = destDirName + "/src/main/java/" + BaseConfig.PACKAGE.replace(".", "/");
        FileUtil.createDir(packagePath);


        //生成pom.xml
        String pomTemplate = FileUtil.readClassFile(Starter.class, "/template/pom.template");
        DefaultPomGenerator pomGenerator = new DefaultPomGenerator();
        String pom = pomGenerator.createPom(pomTemplate, BaseConfig.GROUPID, BaseConfig.ARTIFACTID);
        FileUtil.writeFile(destDirName + "/pom.xml", pom);

        //生成web.xml
        String webXmlTemplate = FileUtil.readClassFile(Starter.class, "/template/webxml.template");
        DefaultWebXmlGenerator webXmlGenerator = new DefaultWebXmlGenerator();
        String webXml = webXmlGenerator.createWebXml(webXmlTemplate, BaseConfig.PACKAGE);
        FileUtil.writeFile(destDirName + "/src/main/webapp/WEB-INF/web.xml", webXml);


        Map<String, List<TableRow>> tableMap = null;
        tableMap = ExcelUtil.getAllSheets(BaseConfig.PATH_EXCEL);
        List<TableBean> tableBeans = TableUtil.parseTables(tableMap);

        //生成sql
        MysqlSQLGenerator mysqlSQLGenerator = new MysqlSQLGenerator();
        for (TableBean tableBean :
                tableBeans) {
            String sql = mysqlSQLGenerator.tableCreateSQL(tableBean);
            try {
                FileUtil.writeFile(destDirName + "/doc/sql/" + tableBean.getName() + ".sql", sql);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        List<File> fileList = FileUtil.getClassFileList(Starter.class, "/template");
        for (File file :
                fileList) {
            String path = file.getPath();
            String fileName = file.getName();
            if (fileName.endsWith(".template")) {
                int begin = 0;
                if (System.getProperty("os.name").contains("Windows")) {
                    begin = path.indexOf("\\template") + "\\template".length();
                } else {
                    begin = path.indexOf("/template") + "/template".length();
                }
                String fileRelativePath = path.substring(begin, path.length());
                String fileContent = FileUtil.readFile(path);
                if (fileName.equals("webxml.template") || fileName.equals("pom.template")) {
                    continue;
                }
                if (fileName.contains("{T}")) {
                    for (TableBean tableBean :
                            tableBeans) {
                        Template template = new Template(fileContent);
                        String modelName = StringUtil.toUpperCase4Index(StringUtil.formatArgsName(tableBean.getName()));
                        template.build(BaseConfig.PACKAGE, modelName + "Model", tableBean.getName(), modelName + "Service", tableBean, tableBeans);
                        FileUtil.writeFile(packagePath + fileRelativePath.replace(".template", ".java").replace("{T}", modelName), template.getContent());
                    }

                } else {
                    Template template = new Template(fileContent);
                    template.build(BaseConfig.PACKAGE, null, null, null, null, tableBeans);
                    FileUtil.writeFile(packagePath + fileRelativePath.replace(".template", ".java"), template.getContent());
                }
            }

        }
    }
}
